<?php

class mysql extends model
{
	function showTables()
	{
		$db = sf::getLib("db");
		$query = $db->query("SHOW TABLES");
		$result = $db->result_array($query);
		foreach($result as $row)
			foreach($row as $val)
				$tables[] = $val;
		return $tables;
	}
	
	function showFields($table='article')
	{
		$db = sf::getLib("db");
		$query = $db->query("DESCRIBE `$table`");
		$result = $db->result_array($query);
		for($i=0,$n=count($result);$i<$n;$i++){
			if($result[$i]['Key']) $fields[$i]['primary_key'] = 1;
			else $fields[$i]['primary_key'] = 0;
			$fields[$i]['name'] = $result[$i]['Field'];		
			$fields[$i]['type'] = $this->getType($result[$i]['Type']);
			$fields[$i]['def'] = $result[$i]['Default'];
		}
		return $fields;
	}
	
	function getType($type='')
	{
		switch(array_shift(explode('(',$type))){
			case 'int':
				return 'int';
			break;
			case 'float':
				return 'float';
			break;
			case 'datetime':
				return 'datetime';
			break;
			case 'binary':
				return 'binary';
			break;
			default:
				return 'string';
			break;
		}
		return $type;	
	}
}
?>